/*
 * ImageDAO.java
 *
 * Created on 14 marzec 2007, 00:27
 *
 */

package arocms.business;

import arocms.util.HibernateUtil;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;

/**
 *
 * @author arek
 */
public class ImageDAO implements DaoMethods<Image> {
    
    private Logger log = Logger.getLogger(this.getClass());
    
    /** Creates a new instance of ImageDAO */
    public ImageDAO() {
    }
    
    public Image createOrUpdate(Image obj) {
        log.debug("--> createOrUpdate");
        if (obj.getId()==null) {
            log.debug("<-- createOrUpdate");
            return create(obj); }
        log.debug("<-- createOrUpdate");
        return update(obj);
    }
    
    public Image create(Image obj) {
        log.debug("--> create");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.persist(obj);
        ses.getTransaction().commit();
        log.debug("<-- create");
        return obj;
    }
    
    public Image update(Image obj) {
        log.debug("--> update");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        
        Image img = (Image) ses.load(Image.class, obj.getId());
        
        img.setTitle(obj.getTitle());
        img.setDescription(obj.getDescription());
        if (obj.getFileSize()>0) {
            img.setContentData(obj.getContentData());
            img.setContentType(obj.getContentType());
            img.setDate(obj.getDate());
            img.setFileName(obj.getFileName());
            img.setFileSize(obj.getFileSize());
        }
        
        //ses.update(img);
        
        ses.getTransaction().commit();
        log.debug("<-- update");
        return obj;
    }
    
    public Image find(Long pk) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        Image ima = (Image) ses.load(Image.class, pk);
        ses.getTransaction().commit();
        return ima;
    }
    
    public List<Image> findAll() {
        log.debug("--> findAll");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        List<Image> imgs = (List<Image>) ses.createQuery("From Image").list();
        ses.getTransaction().commit();
        if (imgs != null) log.debug("returned images count: " + imgs.size());
        else log.error("no images returned");
        log.debug("<-- findAll");
        return imgs;
    }
    
    public boolean delete(Long pk) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.delete(ses.load(Image.class, pk));
        ses.getTransaction().commit();
        return true;
    }
    
    public boolean delete(Image obj) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.delete(obj);
        ses.getTransaction().commit();
        return true;
    }
    
    public InputStream getImageData(Long pk) throws SQLException{
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        Image img = (Image) ses.load(Image.class, pk);
        InputStream imageData = img.getContentData().getBinaryStream();
        ses.getTransaction().commit();
        return imageData;
    }
}
